//
//  NSString+Regex.m
//  MoYou
//
//  Created by she1323 on 16/10/10.
//  Copyright © 2016年 she1323. All rights reserved.
//

#import "NSString+Regex.h"

@implementation NSString (Regex)

+(NSString *)judegeIsNullAndTransform:(NSString *)transstr{
    return transstr == nil ? @"":transstr;
}

#pragma 正则匹配手机号
+ (BOOL)checkTelNumber:(NSString *) telNumber
{
    NSString *pattern = @"^1+[3578]+\\d{9}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:telNumber];
    return isMatch;
}


#pragma 正则匹配用户密码6-18位数字和字母组合
+ (BOOL)checkPassword:(NSString *) password
{
    NSString *pattern = @"^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,18}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:password];
    return isMatch;
    
}
#pragma 正则匹配用户姓名,20位的中文或英文
+ (BOOL)checkUserName : (NSString *) userName
{
    NSString *pattern = @"^[a-zA-Z\u4E00-\u9FA5]{1,20}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:userName];
    return isMatch;
    
}


#pragma 正则匹配用户身份证号15或18位
+ (BOOL)checkUserIdCard: (NSString *) idCard
{
    NSString *pattern = @"(^[0-9]{15}$)|([0-9]{17}([0-9]|X)$)";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:idCard];
    return isMatch;
}
#pragma 正则匹员工号,12位的数字
+ (BOOL)checkEmployeeNumber : (NSString *) number
{
    NSString *pattern = @"^[0-9]{12}";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:number];
    return isMatch;
    
}

#pragma 正则匹配URL
+ (BOOL)checkURL : (NSString *) url
{
    NSString *pattern = @"^[0-9A-Za-z]{1,50}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:url];
    return isMatch;
}
#pragma 设置文字竖直排列
+(NSString *)VerticalStringWithString:(NSString *)string{
    NSMutableString * str = [[NSMutableString alloc] initWithString:string];
    NSInteger count = str.length;
    for (int i = 1; i < count; i ++) {
        [str insertString:@"\n" atIndex:i*2 - 1];
    }
    return str;
}


#pragma mark---根据指定文本,字体和最大高度计算尺寸
+ (CGSize)sizeWidthWidth:(NSString *)text font:(UIFont *)font maxHeight:(CGFloat)height{
    
    NSMutableDictionary *attrDict = [NSMutableDictionary dictionary];
    attrDict[NSFontAttributeName] = font;
    CGSize size = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDict context:nil].size;
    return size;
}


-(BOOL)isValidPhoneNumber{
    NSString *regex = @"1[3|5|7|8|][0-9]{9}";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [predicate evaluateWithObject:self];
}

///*  判断用户输入的密码是否符合规范，符合规范的密码要求：
//1. 长度大于8位
//2. 密码中必须同时包含数字和字母
//*/
- (BOOL)judgePassWordLegal{
    BOOL result = false;
    if ([self length] >= 8 && [self length] <= 20){
        // 判断长度大于8位后再接着判断是否同时包含数字和字符
        NSString * regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        result = [pred evaluateWithObject:self];
    }
    return result;
}

+ (NSString *)isOrNoPasswordStyle:(NSString *)passWordName
{
    
    NSString * message;
    
    if (passWordName.length<8) {
        
        message=@" 密码不能少于8位，请您重新输入";
        
    }
    
    else if (passWordName.length>20)
        
    {
        
        message=@"密码最大长度为20位，请您重新输入";
        
    }
    
    else if ([self JudgeTheillegalCharacter:passWordName])
        
    {
        
        message=@"密码不能包含特殊字符，请您重新输入";
        
    }
    
    else if (![self judgePassWordLegal:passWordName])
        
    {
        message=@"密码必须同时包含字母和数字";
    }
    
    return message;
    
}

+(BOOL)JudgeTheillegalCharacter:(NSString *)content{
    
    //提示标签不能输入特殊字符
    
    NSString *str =@"^[A-Za-z0-9\\u4e00-\u9fa5]+$";
    
    NSPredicate* emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    
    if (![emailTest evaluateWithObject:content]) {
        
        return YES;
    }
    
    return NO;
    
}

+(BOOL)judgePassWordLegal:(NSString *)pass{
    
    BOOL result ;
    
    // 判断长度大于8位后再接着判断是否同时包含数字和大小写字母
    
    NSString * regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    result = [pred evaluateWithObject:pass];
    
    return result;
    
}

/**
 给一个时间，给一个数，正数是以后n个月，负数是前n个月

 @param date 日期
 @param month 月份
 @return 时间
 */
+(NSDate *)getPriousorLaterDateFromDate:(NSDate *)date withMonth:(NSInteger)month
 {

   NSDateComponents *comps = [[NSDateComponents alloc] init];

    [comps setMonth:month];
    NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDate *mDate = [calender dateByAddingComponents:comps toDate:date options:0];

    return mDate;

}
+(NSDate *)getPriousorDayLaterDateFromDate:(NSDate *)date withDay:(NSInteger)day{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    //设置时区,这个对于时间的处理有时很重要
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    
    [formatter setTimeZone:timeZone];
    
    NSDate *currentDate = [NSDate dateWithTimeInterval:day * 24 *60* 60 sinceDate:date];
    return currentDate;
}
+(NSDate *)getDateAccordingTimeStr:(NSString *)timeStr{
     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
     [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
     NSDate *date = [dateFormatter dateFromString:timeStr];
     return date;
}
+(NSString *)getTimeStrAccoringDate:(NSDate *)date{
    NSDateFormatter *formatter1=[[NSDateFormatter alloc] init];
    [formatter1 setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter1 setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter1 setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    //设置时区,这个对于时间的处理有时很重要
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    
    [formatter1 setTimeZone:timeZone];
    //设置格式：zzz表示时区
//    [formatter1 setDateFormat:@"yyyy-MM-dd"];
    //NSDate转NSString
    NSString *currentDateString = [formatter1 stringFromDate:date];
    return currentDateString;
}
+(NSString *)getCurrentTime{
    //获取当前时间日期
    NSDate *date=[NSDate date];
    NSDateFormatter *formatter1=[[NSDateFormatter alloc] init];
    [formatter1 setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter1 setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter1 setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    //设置时区,这个对于时间的处理有时很重要
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    
    [formatter1 setTimeZone:timeZone];
    NSString *dateStr;
    dateStr=[formatter1 stringFromDate:date];
    NSLog(@"%@",dateStr);
    return dateStr;
}
+ (BOOL) isBlankString:(NSString *)string {
    
    if (string == nil || string ==NULL) {
        
        return YES;
        
    }
    
    if ([string isKindOfClass:[NSNull class]]) {
        
        return YES;
        
    }
    
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {//特殊字符判断
        
        return YES;
        
    }
    
    return NO;
    
}
+ (NSString *)stringWithTime:(CGFloat)time;
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:time];
    NSDateFormatter *dateFmt = [[NSDateFormatter alloc] init];
    
    if (time >= 3600) {
        [dateFmt setDateFormat:@"HH:mm:ss"];
    } else {
        [dateFmt setDateFormat:@"mm:ss"];
    }
    return [dateFmt stringFromDate:date];
}
+(NSInteger)getNowTimestamp{
    
    
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    //设置时区,这个对于时间的处理有时很重要
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    
    [formatter setTimeZone:timeZone];
    
    NSDate *datenow = [NSDate date];//现在时间
    
    
    
    NSLog(@"设备当前的时间:%@",[formatter stringFromDate:datenow]);
    
    //时间转时间戳的方法:
    
    
    
    NSInteger timeSp = [[NSNumber numberWithDouble:[datenow timeIntervalSince1970]] integerValue];
    
    
    
    NSLog(@"设备当前的时间戳:%ld",(long)timeSp); //时间戳的值
    
    
    
    return timeSp;
    
}
+(NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:format]; //(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    
    [formatter setTimeZone:timeZone];

    NSDate* date = [formatter dateFromString:formatTime]; //------------将字符串按formatter转成nsdate
    
    //时间转时间戳的方法:
    
    NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
    
    NSLog(@"将某个时间转化成 时间戳&&&&&&&timeSp:%ld",(long)timeSp); //时间戳的值
    
    
    
    return timeSp;
    
}

+(NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:format]; // （@"YYYY-MM-dd hh:mm:ss"）----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    
    [formatter setTimeZone:timeZone];
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];
    
    NSLog(@"1296035591  = %@",confromTimesp);
    
    
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    
    
    
    //NSLog(@"&&&&&&&confromTimespStr = : %@",confromTimespStr);

    
    return confromTimespStr;
    
}
/**
 根据标准时间计算出时间差

 */
/**
 根据时间戳获取到时间差
 
 */
+(NSString* )getHourDiffrent:(NSString *)takeCarTime systemTime:(NSString *)systemTime

{
    
    //  时区相差8个小时 加上这个时区即是北京时间
    
    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
    
    NSInteger delta = [timeZone secondsFromGMT];
    
    NSDate *DRstartDate = [NSDate date];
    NSDate *DRendDate = [[NSDate alloc] initWithTimeIntervalSince1970:[systemTime doubleValue]/1000 + delta];
    NSLog(@"开始时间%@",DRstartDate);
    NSLog(@"结束时间%@",DRendDate);
    // 日历对象 （方便比较两个日期之间的差距）
    
    NSTimeInterval interTime = [DRendDate timeIntervalSinceNow];
    //计算天数、时、分、秒
    
    int days = ((int)interTime)/(3600*24);
    
    int hours = ((int)interTime)%(3600*24)/3600;
    
    int minutes = ((int)interTime)%(3600*24)%3600/60;
    
    NSString *dateContent = [[NSString alloc] initWithFormat:@"倒计时:%i天%i时%i分",days,hours,minutes];

    return dateContent;
    
}

// 字典转json字符串方法

+(NSString *)convertToJsonData:(NSDictionary *)dict

{
    
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    
    NSString *jsonString;
    
    if (!jsonData) {
        
        NSLog(@"%@",error);
        
    }else{
        
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
        
    }
    
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    
    NSRange range = {0,jsonString.length};
    
    //去掉字符串中的空格
    
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    
    NSRange range2 = {0,mutStr.length};
    
    //去掉字符串中的换行符
    
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    
    return mutStr;
    
}

/**
 数组转Json

 @param dict 数组
 @return 返回值
 */
+(NSString *)convertToJsonArray:(NSArray *)dict

{
    
    NSError *error;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    
    NSString *jsonString;
    
    if (!jsonData) {
        
        NSLog(@"%@",error);
        
    }else{
        
        jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
        
    }
    
    NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
    
    NSRange range = {0,jsonString.length};
    
    //去掉字符串中的空格
    
    [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
    
    NSRange range2 = {0,mutStr.length};
    
    //去掉字符串中的换行符
    
    [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
    
    return mutStr;
    
}

/// 根据mcc_jc_id的值返回相对应的标题
/// @param type mcc_jc_id
+(NSString *)backShopTypeAccordingType:(NSString *)type{
    if ([type isEqualToString:@"-1"]) {
        return @"个人";
    }else if ([type isEqualToString:@"1"]){
        return @"个体工商户";
    }else if ([type isEqualToString:@"2"]){
        return @"企业";
    }else if ([type isEqualToString:@"3"]){
        return @"小微商户";
    }else{
        return @"";
    }
}

/// 根据商户的类型名称返回商户的mcc_jc_id值
/// @param title 商户名称
+(NSString *)backTypeAccordingShop:(NSString *)title{
    if ([title isEqualToString:@"个人"]) {
        return @"-1";
    }else if ([title isEqualToString:@"个体工商户"]){
        return @"1";
    }else if ([title isEqualToString:@"企业"]){
        return @"2";
    }else if ([title isEqualToString:@"小微商户"]){
        return @"3";
    }else{
        return @"";
    }
}

/// 根据选中的账户类型文字返回相对应的值
/// @param title 账户类型
+(NSString *)backAccountTypeAccordingTitle:(NSString *)title{
//    1法人账户，2非法人账户，3对公账户，4小微商户
    if ([title isEqualToString:@"法人账户"]) {
        return @"1";
    }else if ([title isEqualToString:@"非法人账户"]){
        return @"2";
    }else if ([title isEqualToString:@"对公账户"]){
        return @"3";
    }else if ([title isEqualToString:@"小微账户"]){
        return @"4";
    }
    return @"";
}

/// 根据账户对应值返回相对应的标题title
/// @param accountType 账户对应值
+(NSString *)backTitleAccordingAccountType:(NSString *)accountType{
    if ([accountType isEqualToString:@"1"]) {
        return @"法人账户";
    }else if ([accountType isEqualToString:@"2"]){
        return @"非法人账户";
    }else if ([accountType isEqualToString:@"3"]){
        return @"对公账户";
    }else if ([accountType isEqualToString:@"4"]){
        return @"小微账户";
    }
    return @"";
}
/// 根据商户状态返回值
/// @param status 状态
+(NSString *)backShopStatusAccordingStatus:(NSString *)status{
    if ([status isEqualToString:@"0"]) {
        return @"待总部审核";
    }else if ([status isEqualToString:@"1"]){
        return @"进件成功";
    }else if ([status isEqualToString:@"2"]){
        return @"总部未过审";
    }else if ([status isEqualToString:@"3"]){
        return @"待分部校验";
    }else if ([status isEqualToString:@"4"]){
        return @"校验不通过";
    }else if ([status isEqualToString:@"5"]){
        return @"待提交资料";
    }else if ([status isEqualToString:@"6"]){
        return @"暂存信息";
    }else if ([status isEqualToString:@"7"]){
        return @"待银行开户";
    }else if ([status isEqualToString:@"8"]){
        return @"进件失败";
    }else if ([status isEqualToString:@"9"]){
        return @"进件中";
    }else{
        return @"";
    }
    
}
/// 根据支付流水的付款渠道Mode返回相对应的值
/// @param mode 付款渠道
+(NSString *)backPayDataModeStrAccordingModel:(NSString *)mode{
    if ([mode isEqualToString:@"台卡"]) {
        return @"0";
    }else if ([mode isEqualToString:@"收银APP"]){
        return @"1";
    }else if ([mode isEqualToString:@"百富"]){
        return @"12";
    }
    return @"";
}
/// 根据支付流水的付款渠道字符串Mode返回相对应的值
/// @param modestr 付款渠道
+(NSString *)backPayDataModeAccordingModelStr:(NSString *)modestr{
    if ([modestr isEqualToString:@"0"]) {
        return @"台卡";
    }else if ([modestr isEqualToString:@"1"]){
        return @"收银APP";
    }else if ([modestr isEqualToString:@"12"]){
        return @"百富";
    }
    return @"";
}

/// 根据支付方式返回支付方式字符串
/// @param payStyle 支付方式
+(NSString *)backPayDataStyleStrAccordingModelStr:(NSString *)payStyle{
    if ([payStyle isEqualToString:@"1"]) {
        return @"微信";
    }else if ([payStyle isEqualToString:@"2"]){
        return @"支付宝";
    }else if ([payStyle isEqualToString:@"3"]){
        return @"刷储蓄卡或信用卡";
    }else if ([payStyle isEqualToString:@"4"]){
        return @"储值支付";
    }else if ([payStyle isEqualToString:@"5"]){
        return @"现金支付";
    }else if ([payStyle isEqualToString:@"6"]){
        return @"联名卡";
    }else if ([payStyle isEqualToString:@"7"]){
        return @"银行卡";
    }else if ([payStyle isEqualToString:@"8"]){
        return @"库分期(信用卡分期)";
    }else if ([payStyle isEqualToString:@"10"]){
        return @"银联NFC支付";
    }
    else if ([payStyle isEqualToString:@"9"]){
        return @"云闪付";
    }
    return @"";
}

/// 根据支付方式字符串返回支付方式
/// @param payStyleStr 支付方式字符串
+(NSString *)backPayDataStyleAccordingModelStr:(NSString *)payStyleStr{
    if ([payStyleStr isEqualToString:@"微信"]) {
        return @"1";
    }else if ([payStyleStr isEqualToString:@"支付宝"]){
        return @"2";
    }else if ([payStyleStr isEqualToString:@"刷储蓄卡或信用卡"]){
        return @"3";
    }else if ([payStyleStr isEqualToString:@"储值支付"]){
        return @"4";
    }else if ([payStyleStr isEqualToString:@"现金支付"]){
        return @"5";
    }else if ([payStyleStr isEqualToString:@"联名卡"]){
        return @"6";
    }else if ([payStyleStr isEqualToString:@"银行卡"]){
        return @"7";
    }else if ([payStyleStr isEqualToString:@"库分期(信用卡分期)"]){
        return @"8";
    }else if ([payStyleStr isEqualToString:@"银联NFC支付"]){
        return @"10";
    }
    else if ([payStyleStr isEqualToString:@"云闪付"]){
        return @"9";
    }
    return @"";
}

/// 根据支付状态返回相对应的字符串的值
/// @param payStatu 支付状态
+(NSString *)backPayDataStatuStrAccordingModelStr:(NSString *)payStatu{
//    0未支付,-1支付中，-2失败,1支付成功  2退款成功 3退款失败 4退款中   6预授权7,交易关闭
    if ([payStatu isEqualToString:@"0"]) {
        return @"未支付";
    }else if ([payStatu isEqualToString:@"-1"]){
        return @"支付中";
    }else if ([payStatu isEqualToString:@"-2"]){
        return @"支付失败";
    }else if ([payStatu isEqualToString:@"1"]){
        return @"支付成功";
    }else if ([payStatu isEqualToString:@"2"] || [payStatu isEqualToString:@"5"]){
        return @"退款成功";
    }else if ([payStatu isEqualToString:@"3"]){
        return @"退款失败";
    }else if ([payStatu isEqualToString:@"4"]){
        return @"退款中";
    }else if ([payStatu isEqualToString:@"6"]){
        return @"预授权";
    }else if ([payStatu isEqualToString:@"7"]){
        return @"交易关闭";
    }
    return @"";
}
@end
